package pri.zxx.webdemo.entity.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import pri.zxx.learndemo.localUtils.LocalDateUtil;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author Twilight
 * @desc
 * @createTime 2020-05-17-10:07
 */
@Tag(name = "结果数据")
public class ResultData<T> {
	@Schema(name = "数据")
	private T data;
	@Schema(name = "返回码")
	private Integer code;
	@Schema(name = "提示")
	private String msg;
	@Schema(name = "时间戳")
	private Long time;
	@Schema(name = "格式化时间")
	private String now;
	@Schema(name = "分页数据")
	private PageDto pageDto;


	public ResultData(ResultEnum resultEnum) {
		LocalDateTime now = LocalDateTime.now();
		this.code = resultEnum.getCode();
		this.msg = resultEnum.getMsg();
		this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
		this.now = LocalDateUtil.formatter(now);
	}

	public ResultData(ResultEnum resultEnum, T data) {
		LocalDateTime now = LocalDateTime.now();
		this.data = data;
		this.code = resultEnum.getCode();
		this.msg = resultEnum.getMsg();
		this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
		this.now = LocalDateUtil.formatter(now);
	}

	public ResultData(ResultEnum resultEnum, String msg) {
		LocalDateTime now = LocalDateTime.now();
		this.code = resultEnum.getCode();
		this.msg = msg;
		this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
		this.now = LocalDateUtil.formatter(now);
	}

	public ResultData(Integer code, String msg, T data) {
		LocalDateTime now = LocalDateTime.now();
		this.data = data;
		this.code = code;
		this.msg = msg;
		this.time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
		this.now = LocalDateUtil.formatter(now);
	}

	public ResultData() {
	}

	public static ResultData success() {
		return new ResultData(ResultEnum.SUCCESS);
	}

	public static ResultData success(String msg) {
		return new ResultData(ResultEnum.SUCCESS, msg);
	}

	public static <T> ResultData success(T data) {
		return new ResultData<T>(ResultEnum.SUCCESS, data);
	}

	public static ResultData fail() {
		return new ResultData(ResultEnum.FAILED);
	}

	public static ResultData fail(String msg) {
		return new ResultData(ResultEnum.FAILED, msg);
	}

	public static <T> ResultData fail(T data) {
		return new ResultData(ResultEnum.FAILED, data);
	}

	public static <T> ResultData custom(Integer code, String msg, T data) {
		return new ResultData(code, msg, data);
	}

	public ResultData page(PageDto pageDto) {
		this.pageDto = pageDto;
		return this;
	}

	public ResultData defaultPage() {
		this.pageDto = new PageDto(1, 10);
		return this;
	}

	public String getNow() {
		return now;
	}

	public void setNow(String now) {
		this.now = now;
	}

	public Long getTime() {
		return time;
	}

	public void setTime(Long time) {
		this.time = time;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public PageDto getPageDto() {
		return pageDto;
	}

	public void setPageDto(PageDto pageDto) {
		this.pageDto = pageDto;
	}
}
